// pages/staff_mine/edit_address/index.js

let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        region_list: new Array(),
        can_apply: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {

        // 验证登录
        app.verifyLogin();

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

        // 获取数据
        app.execute(function () {
            let page = app.getPage();
            page.getRegion(page);
        });
    },

    /**
     * 自定义函数--获取范围
    */
    getRegion: function(page){
        wx.showLoading({
            title: '获取信息...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_region/get_info',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'GET',
            success: function (response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        page.setData({
                            region_list: response.data.data.region_list,
                            can_apply: response.data.data.can_apply
                        });
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--控制跳转
    */
    navigateToApply: function (){
        if(true === this.data.can_apply){
            wx.navigateTo({
                url: '/pages/staff_mine/edit_address/apply',
            });
        }else{
            wx.showToast({
                title: '您的申请正在审核中...',
                icon: 'none'
            });
        };
    },

    /**
     * 自定义函数--显示菜单
    */
    showItemMenu: function (e) {
        let page = this;
        let data_id = e.currentTarget.dataset.id;
        wx.showActionSheet({
            itemList: ['删除'],
            success: function(e) {
                switch(e.tapIndex) {
                    case 0 :    // 删除
                        page.removeRegion(data_id);
                        break;
                };
            }
        });
    },

    /**
     * 自定义函数--移除地区
    */
    removeRegion: function(data_id) {
        let page = this;
        wx.showModal({
            title: '提示',
            content: '确认要进行删除操作吗？',
            success: function(e) {
                switch (e.confirm){
                    case true:
                        let data = {
                            region_id: data_id
                        };
                        wx.showLoading({
                            title: '提交中...',
                        });
                        wx.request({
                            url: app.data.service_url + 'staff_recycle_region/remove',
                            header: {
                                'Client-Token': app.data.client_token,
                            },
                            method: 'POST',
                            data: data,
                            success: function (response) {
                                wx.hideLoading();
                                if (200 === response.statusCode) {
                                    if (9 === response.data.errno) {
                                        wx.redirectTo({
                                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                                        });
                                    } else if (0 === response.data.errno) {
                                        page.getRegion(page);
                                    } else {
                                        wx.showToast({
                                            title: response.data.message,
                                            icon: 'none'
                                        });
                                    };
                                } else {
                                    wx.showToast({
                                        title: '远程服务返回错误',
                                        icon: 'none'
                                    });
                                };
                            },
                            fail: function () {
                                wx.hideLoading();
                                wx.showToast({
                                    title: '连接远程服务失败',
                                    icon: 'none'
                                });
                            }
                        });
                        break;
                };
            }

        })
    }

});